package com.AuthServer.controller;

import com.AuthServer.common.Result;
import com.AuthServer.pojo.entity.Teacher;
import com.AuthServer.pojo.entity.User;
import com.AuthServer.pojo.form.*;
import com.AuthServer.pojo.query.UserQuery;
import com.AuthServer.pojo.vo.UserInfoVo;
import com.AuthServer.service.TeacherService;
import com.AuthServer.service.U_PService;
import com.AuthServer.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.mail.internet.AddressException;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("/s0/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private U_PService uPService;


    @PostMapping("/login")
    public Result<String> login(@RequestBody @Valid LoginForm loginForm) throws JsonProcessingException {
        return userService.login(loginForm);
    }

    @GetMapping(value = "/getImg_code", params = "userId")
    public Result<String> getGraphicalCaptcha(@RequestParam String userId) throws IOException {
        return userService.getGraphicalCaptcha(userId);
    }

    @GetMapping(value = "/getEma_code", params = "email")
    public Result<?> getEmailCaptcha(@RequestParam String email) throws AddressException, UnsupportedEncodingException {
        return userService.getEmailCaptcha(email);
    }

    @PutMapping("/updatePassword")
    public Result<?> updatePassword(@RequestBody UpdatePasswordForm updatePasswordForm) throws JsonProcessingException {
        return userService.updatePassword(updatePasswordForm);
    }
    @PostMapping("/verifyInfo")
    public Result<String> verifyInfo(@RequestBody @Valid VerifyInfoForm verifyInfoForm) {
        Result<Teacher> result = teacherService.getOne(verifyInfoForm.getUserId());
        Teacher teacher = result.getData();
        return userService.verifyInfo(verifyInfoForm, teacher);
    }

    @PostMapping("/bindEmail")
    public Result<String> bindEmail(@RequestBody @Valid EmailForm emailForm) {
        return userService.bindEmail(emailForm);
    }

    @PostMapping("/bindEma_code")
    public Result<?> bindEmailCaptcha(@RequestBody BindCodeForm bindCodeForm){
        return userService.bindEmailCaptcha(bindCodeForm);
    }

    @PostMapping("/setPassword")
    public Result<?> setPassword(@RequestBody @Valid PasswordForm passwordForm) {
        return userService.setPassword(passwordForm);
    }

    @GetMapping("/getUserList")
    public Result<?> getUserList(@RequestParam("pageNumber")Long pageNumber,@RequestParam("pageSize") Long pageSize) {
        return userService.getUserList(pageNumber, pageSize);
    }

    @GetMapping("/getUserPerm")
    public Result<?> getUserPerm(@RequestParam("userId") String userId) {
        return userService.getUserPerm(userId);
    }

    @GetMapping("/addUserPerm")
    public Result<?> addUserPerm(String userId, Integer[] permIds) {
        return userService.addUserPerm(userId,permIds);
    }

    @GetMapping("/getPerm")
    public Result<?> getPerm() {
        return userService.getPerm();
    }

    @GetMapping("/deleteUserPerm")
    public Result<?> deleteUserPerm(String userId, Integer[] permIds) {
        return userService.deleteUserPerm(userId,permIds);
    }

    @GetMapping("/getUserInfo")
    public Result<UserInfoVo> getUserInfo(@RequestParam @Valid String userId) {
        return userService.getOneByUserId(userId);
    }

    @PostMapping("/updateStatus")
    public Result<?> updateStatus(String userId,String status) {
        return userService.updateStatus(userId,status);
    }

    @PostMapping("/searchUserList")
    public Result<?> searchUserList(@RequestBody UserQuery userQuery){return userService.searchUserList(userQuery);}

    @DeleteMapping("/removeUser")
    public Result<?> removeUserById(@RequestParam String userId){return userService.removeUserById(userId);}

    @DeleteMapping("/removeBatchUsers")
    public Result<?> removeBatchUsers(@RequestBody List<String> userIds) {return userService.removeBatchUsers(userIds);}

    @PostMapping("/verifyEmail")
    public Result<?> verifyEmail(@RequestBody BindEmailForm bindEmailForm) throws JsonProcessingException {
        return userService.verifyEmail(bindEmailForm);
    }

    @PostMapping("/addBatchAdmin")
    public Result<?> addBatchAdmin(@RequestBody List<String> userIds) {
        return uPService.addBatchAdmin(userIds);
    }
}
